import { SpriteFrame } from 'cc'

const INDEX_REG = /\((\d+)\)/

const getNumberWithinString = (str: string) => parseInt(str.match(INDEX_REG)?.[1] || '0')
// 帧画面排序
export const sortSpriteFrame = (spriteFrame: Array<SpriteFrame>) => spriteFrame.sort((a, b) => getNumberWithinString(a.name) - getNumberWithinString(b.name))
// 弧角转换
export const radToAngle = (rad: number) => (rad / Math.PI) * 180
// 深克隆
export const deepClone = (obj: any) => JSON.parse(JSON.stringify(obj))

export const deepClone2 = (obj: any) => {
  if (typeof obj !== 'object' || obj === null) return obj
  const res = Array.isArray(obj) ? [] : {}
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      res[key] = deepClone(obj[key])
    }
  }
  return res
}

// 生成伪随机
// 取模返回一个从0-233280间的数
export const randomBySeed=(seed:number)=>(seed*9301+49297)%233280

